package com.vehicle.common.util.thread.pool;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;

/**
 * Created by yantingbin on 2017/7/29.
 */
public final class ThreadPoolManager {

    public static final ThreadPoolManager I = new ThreadPoolManager();

    private final ExecutorFactory executorFactory=ExecutorFactory.create();

    //private final ExecutorFactory executorFactory = SpringUtil.getApplicationContext().getBean(ExecutorFactory.class);

    private final Map<String, Executor> pools = new ConcurrentHashMap<>();

    public Executor getEventWorkerExecutor() {
        return pools.computeIfAbsent("event_worker_mq", s -> executorFactory.get(ExecutorFactory.EVENT_WORKER_MQ));
    }

    public Executor getEventPublishExecutor(){
        return pools.computeIfAbsent("event_publish_mq",s->executorFactory.get(ExecutorFactory.EVENT_PUBLISH_MQ));
    }

    public void register(String name, Executor executor) {
        Objects.requireNonNull(name);
        Objects.requireNonNull(executor);
        pools.put(name, executor);
    }
}
